package com.iotechn.unimall.admin.api.dashboard;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.OrderDO;
import com.iotechn.unimall.data.domain.SpuDO;
import com.iotechn.unimall.data.dto.DashboardIntegralDTO;
import com.iotechn.unimall.data.entity.BookDo;
import com.iotechn.unimall.data.entity.HomePage;
import com.iotechn.unimall.data.entity.StudentDo;
import com.iotechn.unimall.data.enums.OrderStatusType;
import com.iotechn.unimall.data.mapper.OrderMapper;
import com.iotechn.unimall.data.mapper.SpuMapper;
import com.iotechn.unimall.data.model.KVModel;
import com.iotechn.unimall.data.privateMapper.BookMapper;
import com.iotechn.unimall.data.privateMapper.BorrowMapper;
import com.iotechn.unimall.data.privateMapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by rize on 2019/7/15.
 */
@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private BorrowMapper borrowMapper;

    @Override
    public Object integral(Long adminId) throws ServiceException {
        DashboardIntegralDTO dto = new DashboardIntegralDTO();
//        QueryWrapper<OrderDO>().eq。获取订单状态为“WAIT_STOCK”的订单总数，
        Integer orderWaitStock = orderMapper.selectCount(new QueryWrapper<OrderDO>().eq("status", OrderStatusType.WAIT_STOCK.getCode()));
//        查询unimall_spu所有商品数量
        Integer spuCount = spuMapper.selectCount(new QueryWrapper<SpuDO>());
//        将unimall_order表中数据按地域数量
        List<KVModel<String, Long>> area = orderMapper.selectAreaStatistics();
//        将unimall_order表中数据按渠道数量
        List<KVModel<String, Long>> channel = orderMapper.selectChannelStatistics();

        dto.setArea(area);
        dto.setChannel(channel);
        dto.setWaitStockCount(orderWaitStock);
        dto.setGoodsCount(spuCount);
        Integer days = 7;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
//        计算当前系统时间的前7天的开始时间
        String startDay = sdf.format(new Date(System.currentTimeMillis() - 1000l * 60 * 60 * 24 * days));
//        查询7天前的订单数，并按创建时间分组
        List<KVModel<String, Long>> orderCountKVList = orderMapper.selectOrderCountStatistics(startDay);
//        查询7天前的交易金额，并按创建时间分组
        List<KVModel<String, Long>> orderSumKVList = orderMapper.selectOrderSumStatistics(startDay);

        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        List<Object[]> orderCount = new LinkedList<>();
//        日期列表
        Object[] orderCountNameArray = new Object[days];
//        单日订单数量
        Object[] orderCountValueArray = new Object[days];
//        将对象依次添加到列表中
        orderCount.add(orderCountNameArray);
        orderCount.add(orderCountValueArray);
        dto.setDaysOrder(orderCount);
        List<Object[]> orderSum = new LinkedList<>();
        Object[] orderSumNameArray = new Object[days];
        Object[] orderSumValueArray = new Object[days];
        orderSum.add(orderSumNameArray);
        orderSum.add(orderSumValueArray);
        dto.setDaysSum(orderSum);
        //这里是在补全 group by 为 0 的情况
        for (int i = 0; i < days; i++) {
            Date date = new Date(System.currentTimeMillis() - 1000l * 60 * 60 * 24 * i);//从当前日期前推
            String key = sdfDay.format(date);
//            以日期为索引来查询当前日期的交易量，
            int i1 = orderCountKVList.indexOf(new KVModel<>(key, null));
            if (i1 >= 0) {
                orderCountNameArray[days - i - 1] = key;
                orderCountValueArray[days - i - 1] = orderCountKVList.get(i1).getValue();
            } else {
                orderCountNameArray[days - i - 1] = key;
                orderCountValueArray[days - i - 1] = 0;
            }
//              类似订单统计，7日分别的当日交易金额
            int i2 = orderSumKVList.indexOf(new KVModel<>(key, null));
            if (i2 >= 0) {
                orderSumNameArray[days - i - 1] = key;
                orderSumValueArray[days - i - 1] = orderSumKVList.get(i2).getValue();
            } else {
                orderSumNameArray[days - i - 1] = key;
                orderSumValueArray[days - i - 1] = 0;
            }
        }
        return dto;
    }

    @Override
    public Object getData() {
        HomePage homePage = new HomePage();
//        查询图书总数
        Integer bookCount = bookMapper.selectCount(new QueryWrapper<BookDo>());
//        查询用户数量
        Integer userCount = studentMapper.selectCount(new QueryWrapper<StudentDo>());
//        将b_student表中数据按专业数量
        List<KVModel<String, Long>> major = studentMapper.selectMajorStatistics();
//        将b_student表中数据按年龄数量
        List<KVModel<String, Long>> age = studentMapper.selectAgeStatistics();
        homePage.setMajor(major);
        homePage.setAge(age);
        homePage.setUserCount(bookCount);
        homePage.setBookCount(userCount);
        Integer days = 7;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
//        计算当前系统时间的前7天的开始时间
        String startDay = sdf.format(new Date(System.currentTimeMillis() - 1000l * 60 * 60 * 24 * days));
//        查询7天前的订单数，并按创建时间分组
        List<KVModel<String, Long>> borrowCountKVList = borrowMapper.selectBorrowCountStatistics(startDay);
//        查询7天前的交易金额，并按创建时间分组
        List<KVModel<String, Long>> outTimeKVList = borrowMapper.selectOutTimeCountStatistics(startDay);

        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        List<Object[]> borrowCount = new LinkedList<>();
//        日期列表
        Object[] borrowCountNameArray = new Object[days];
//        单日订单数量
        Object[] borrowCountValueArray = new Object[days];
//        将对象依次添加到列表中
        borrowCount.add(borrowCountNameArray);
        borrowCount.add(borrowCountValueArray);
        homePage.setDaysBorrow(borrowCount);
        List<Object[]> outTimeCount = new LinkedList<>();
        Object[] outTimeNameArray = new Object[days];
        Object[] outTimeValueArray = new Object[days];
        outTimeCount.add(outTimeNameArray);
        outTimeCount.add(outTimeValueArray);
        homePage.setDaysOutTime(outTimeCount);
        //这里是在补全 group by 为 0 的情况
        for (int i = 0; i < days; i++) {
            Date date = new Date(System.currentTimeMillis() - 1000l * 60 * 60 * 24 * i);//从当前日期前推
            String key = sdfDay.format(date);
//            以日期为索引来查询当前日期的交易量，
            int i1 = borrowCountKVList.indexOf(new KVModel<>(key, null));
            if (i1 >= 0) {
                borrowCountNameArray[days - i - 1] = key;
                borrowCountValueArray[days - i - 1] = borrowCountKVList.get(i1).getValue();
            } else {
                borrowCountNameArray[days - i - 1] = key;
                borrowCountValueArray[days - i - 1] = 0;
            }
//              类似订单统计，7日分别的当日交易金额
            int i2 = outTimeKVList.indexOf(new KVModel<>(key, null));
            if (i2 >= 0) {
                outTimeNameArray[days - i - 1] = key;
                outTimeValueArray[days - i - 1] = outTimeKVList.get(i2).getValue();
            } else {
                outTimeNameArray[days - i - 1] = key;
                outTimeValueArray[days - i - 1] = 0;
            }
        }
        return homePage;
    }

}
